package com.qingke.note.command_impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.qingke.note.Console;
import com.qingke.note.IDao.LabelMapper;
import com.qingke.note.IDao.NoteMapper;
import com.qingke.note.command.Command;
import com.qingke.note.pojo.Label;
import com.qingke.note.pojo.Note;
import com.qingke.note.pojo.Type;
import com.qingke.note.pojo.User;

public class NewCommand extends Command {

	public NewCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		
		if(currentUser==null){
			Console.println("您还未登录，不能执行此操作");
			return;
		}
		SqlSession session = getSession();
		NoteMapper noteMapper = session.getMapper(NoteMapper.class);
		LabelMapper labelMapper = session.getMapper(LabelMapper.class);
		Console.println("======开始新建备忘======");
		String title;
		while(true){
			title = Console.askInput("输入备忘标题");
			if(!title.equals("")){
				break;
			}else{
				Console.print("不能为空，请重新");
			}
		}
		String content;
		while(true){
			content = Console.askInput("输入备忘内容");
			if(!content.equals("")){
				break;
			}else{
				Console.print("不能为空，请重新");
			}
		}
		
		String time;
		Date date;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		while(true){
			time = Console.askInput("输入提醒时间（年-月-日 时:分）");
			try {
				date = sdf.parse(time);
				break;
			} catch (ParseException e) {
				Console.print("时间格式不正确，请重新");
			}
			
		}
		
		Console.println("请选择标签");
		List<Label> labels = labelMapper.selectAllLabels();
		Label label = chooseLabel(labels);
		Note newNote = new Note();
		newNote.setTitle(title);
		newNote.setContent(content);
		Date createTime = new Date();
		newNote.setCreatetime(createTime);
		newNote.setTime(date);
		newNote.setEffective("Y");
		newNote.setLabelId(label.getId());
		newNote.setLabel(label);
		newNote.setStatusId(1);
		newNote.setUser(currentUser);
		newNote.setUserId(currentUser.getId());
		noteMapper.insert(newNote);
		session.commit();
		newNote = noteMapper.selectByTime(createTime);
		currentUser = new LoginCommand(currentUser).login();
		Console.println("新建备忘成功");
		session.close();
		
	}

	private Label chooseLabel(List<Label> labels) {
		// TODO Auto-generated method stub
		int index = 1;
		for (Label label : labels) {
			Console.print("【"+index+"】");
			Console.println(label);
			index++;
		}
		Console.println("【"+index+"】 新建一个标签");
		int choice = Console.askInputInt("请输入序号");
		Label label;
		while(true){
			if(choice==index){
				label = createLabel();
				break;
			}else if(choice<1||choice>index){
				choice = Console.askInputInt("序号不正确，请重新输入");
				continue;
			}else{
				label = labels.get(choice-1);
				break;
			}
		}
		
		return label;
		
	}

	private Label createLabel() {
		String labelName = Console.askInput("标签名");
		Type type = new UpdateCommand(currentUser).chooseType();
		Label label = new Label();
		label.setEffective("Y");
		label.setName(labelName);
		label.setType(type); 
		label.setTypeId(type.getId()); 
		SqlSession session = getSession();
		LabelMapper labelMapper = session.getMapper(LabelMapper.class);
		labelMapper.insert(label);
		List<Label> labels = labelMapper.selectAllLabels();
		for (Label label2 : labels) {
			if(label2.getName().equals(labelName)&&label2.getTypeId()==type.getId()){
				label = label2;
				break;
			}
		}
		session.close();
		return label;
	}

}
